package cn.hjd.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.gzsxt.vo.MsgObj;
import cn.hjd.po.ApproveList;
import cn.hjd.po.PageResult;
import cn.hjd.query.ApproveListQuery;
import cn.hjd.service.DelayService;
import cn.hjd.service.impl.DelayServiceImpl;
import cn.hjd.vo.MsgHint;

@WebServlet("/delay.gzsxt")
public class ApproveListController extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//引入service层 
	private DelayService service = new DelayServiceImpl();
	
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//设置请求编码
		req.setCharacterEncoding("utf-8");
		//设置响应编码和MIME类型
		resp.setContentType("application/json;charset=utf8");
		
		//1.接受标识参数 cmd 用于判断不同的功能
		String cmd = req.getParameter("cmd");
		System.out.println("------"+cmd);
		if ("approve".equals(cmd)) {
			approve(req,resp);
		}else if ("updateUser".equals(cmd)) {
			updateUser(req,resp);
		}else if("addform".equals(cmd)){
			addform(req,resp);
			System.out.println(cmd);
		}else if("delDeyUser".equals(cmd)){
			delectUser(req,resp);
		}
		
		
		
		
		
	}
	//根据用户id 删除指定的用户
	private void delectUser(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException{
		//1.接受需要删除的用户id
		String id = req.getParameter("id");
		//判断id是否为空
		if(id !=null && !"".equals(id.trim())){
			//TODO正则判断 Pattern类
			//2.调用业务层的删除方法
			int row = service.deleteUser(Integer.valueOf(id));
			//创建消息对象
			MsgHint msgHint = null;
			if(row == 1){//删除成功
				msgHint = new MsgHint(1, "删除成功");
				System.out.println("row-------"+row);
			}else{
				msgHint = new MsgHint(0, "删除失败");
				System.out.println("row-------"+row);
			}
			//将处理结果响应给前台
			resp.getWriter().print(JSON.toJSONString(msgHint));
		}
		
	}
	private void addform(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException{
		//调用请求参数封装对象的方法
		ApproveList approveList = this.addformobj(req);
		
		//调用service的保存业务方法
		int row = service.save(approveList);
		System.out.println("---------");
		System.out.println(row);
		//创建消息对象
		MsgHint msgHint = null;
		if (row == 1) {
			msgHint = new MsgHint(1,"恭喜添加用户成功");
		}else {
			msgHint = new MsgHint(0,"服务器正在修复");
		}
		//响应对象
		resp.getWriter().print(JSON.toJSON(msgHint));
	}
	private void updateUser(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		//1.调用请求参数封装对象的方法
		ApproveList approveList = this.getParameter2Obj(req);
		//2.调用业务层修改方法
		
		int row = service.updateApprove(approveList);
		//创建消息对象
		MsgHint msgHint = null;
		if (row == 1) {//修改成功
			msgHint = new MsgHint(1,"批复成功");
		}else {
			msgHint = new MsgHint(0,"批复失败");
		}
		//将处理结果响应给前台
		resp.getWriter().print(JSON.toJSON(msgHint));
	}
	
	
	//查询任务延期审批列表
	private void approve(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.接受各种条件参数：关键字（条件搜索），页面，每页条数
		//用户如果没有提交此参数：接受就是null
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
		//关键字
		String keyword = req.getParameter("keyword");
		
		
		ApproveListQuery qo = new ApproveListQuery();
		if (currentPage != null) {
			qo.setCurrentPage(Integer.valueOf(currentPage));
		}
		if (pageSize != null) {
			qo.setPageSize(Integer.valueOf(pageSize));
		}
		if (keyword != null) {
			qo.setKeyword(keyword);
		}
		//2调用service业务方法查询对应的用户列表
		PageResult pageResult = service.list(qo);
		
		//3共享数据
		req.setAttribute("pageResult", pageResult);
		req.setAttribute("keyword", qo.getKeyword());
		
	
		
		System.out.println(pageResult);
		//4 使用请求转发跳转到 
		req.getRequestDispatcher("/task_management/delay_approval.jsp").forward(req, resp);;
	}
	public ApproveList getParameter2Obj(HttpServletRequest req) {
		
		// 1.接受参数  申请状态 延期到时间  详细意见  封装成ApproveList对象
		String status = req.getParameter("status");
		String replytime = req.getParameter("replytime");
		String delayopinion = req.getParameter("delayopinion");
		String id = req.getParameter("id");
		ApproveList approveList = new ApproveList();
		if (status.equals("同意")) {
			approveList.setStatus(1);
		}else{
			approveList.setStatus(2);
		}
		if (id != null && !"".equals(id.trim())) {
			approveList.setId(Integer.valueOf(id));
		}
		approveList.setDelayopinion(delayopinion);
		
		try {
			// 格式化日期
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date date = format.parse(replytime);
			approveList.setReplytime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return approveList;
	}
	
	public ApproveList addformobj(HttpServletRequest req) {
		// 1.接受参数  
		String title = req.getParameter("addtitle");
		String content = req.getParameter("addcontent");
		String applyclerk = req.getParameter("addapplyclerk");
		String applytime = req.getParameter("addapplytime");
		String delayfintime = req.getParameter("adddelayfintime");
		String delayreason = req.getParameter("adddelayreason");
		
		ApproveList approveList = new ApproveList();
		approveList.setTitle(title);
		approveList.setContent(content);
		approveList.setApplyclerk(applyclerk);
		
		//DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			// 格式化日期
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date date1 = format.parse(delayfintime);
			Date date = format.parse(applytime);
			approveList.setApplytime(date);
			
			//Date date1 = format.parse(delayfintime);
			approveList.setDelayfintime(date1);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		
		approveList.setDelayreason(delayreason);
		
		return approveList;
		
	}
}
